package com.senlingspace.payment.douyin.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.payment.douyin.bean.request.DouyinOrderRequest;
import com.senlingspace.payment.douyin.bean.request.DouyinProductAuditRequest;
import com.senlingspace.payment.douyin.service.DouYinOrderService;
import com.senlingspace.system.domain.SlkjDouyinRecall;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkjDouyinRecallMapper;

import com.senlingspace.payment.douyin.service.ISlkjDouyinRecallService;

import java.util.ArrayList;
import java.util.List;

/**
 * 抖音回调数据记录Service业务层处理
 *
 * @author SenlingSpace
 * @date 2024-10-22
 */
@Slf4j
@Service
public class SlkjDouyinRecallServiceImpl implements ISlkjDouyinRecallService {
    @Autowired
    private SlkjDouyinRecallMapper slkjDouyinRecallMapper;
    @Autowired
    DouYinOrderService douYinOrderService;
    /**
     * 查询抖音回调数据记录
     *
     * @param id 抖音回调数据记录主键
     * @return 抖音回调数据记录
     */
    @Override
    public DouyinProductAuditRequest selectSlkjDouyinRecallById(Long id) {
        SlkjDouyinRecall slkjDouyinRecall = slkjDouyinRecallMapper.selectSlkjDouyinRecallById(id);
        DouyinProductAuditRequest douyinProductAuditRequest = new DouyinProductAuditRequest();
        BeanUtils.copyProperties(slkjDouyinRecall, douyinProductAuditRequest);
        JSONObject jsonObject = JSON.parseObject(slkjDouyinRecall.getContent());
//        douyinProductAuditRequest.setContent(jsonObject);
        return douyinProductAuditRequest;
    }

    /**
     * 查询抖音回调数据记录列表
     *
     * @param douyinProductAuditRequest 抖音回调数据记录
     * @return 抖音回调数据记录
     */
    @Override
    public List<DouyinProductAuditRequest> selectSlkjDouyinRecallList(DouyinProductAuditRequest douyinProductAuditRequest) {

        SlkjDouyinRecall douyinRecallSelect = new SlkjDouyinRecall();
        BeanUtils.copyProperties(douyinProductAuditRequest, douyinRecallSelect);
        String contentStr = JSON.toJSONString(douyinProductAuditRequest.getContent());
        douyinRecallSelect.setContent(contentStr);
        List<SlkjDouyinRecall> slkjDouyinRecalls = slkjDouyinRecallMapper.selectSlkjDouyinRecallList(douyinRecallSelect);
        ArrayList<DouyinProductAuditRequest> douyinProductAuditRequests = new ArrayList<>();
        for (SlkjDouyinRecall douyinRecall : slkjDouyinRecalls) {
            DouyinProductAuditRequest douyinProductAuditRequestRes = new DouyinProductAuditRequest();
            BeanUtils.copyProperties(douyinRecall, douyinProductAuditRequestRes);
            douyinProductAuditRequests.add(douyinProductAuditRequestRes);
        }


        return douyinProductAuditRequests;
    }

    /**
     * 新增抖音回调数据记录
     *
     * @param douyinProductAuditRequest 抖音回调数据记录
     * @return 结果
     */
    @Override
    public int insertSlkjDouyinRecall(DouyinProductAuditRequest douyinProductAuditRequest) {
        douyinProductAuditRequest.setCreateTime(DateUtils.getNowDate());
        SlkjDouyinRecall slkjDouyinRecall = new SlkjDouyinRecall();
        BeanUtils.copyProperties(douyinProductAuditRequest, slkjDouyinRecall);
//        slkjDouyinRecall.setContent(JSON.toJSONString(douyinProductAuditRequest.getContent()));

        try {
            slkjDouyinRecallMapper.insertSlkjDouyinRecall(slkjDouyinRecall);
        } catch (DuplicateKeyException e) {
            log.info("抖音回调记录接口，数据插入重复：{}", douyinProductAuditRequest.toString());
            return 0;
        }

//   订单消息通知
        if ("life_trade_order_notify".equals(slkjDouyinRecall.getEvent())) {

            JSONObject orderJson = JSONObject.parseObject(slkjDouyinRecall.getContent());
            log.info("订单回调接口数据：{}", orderJson);
//            支付成功，创建本系统订单
            if ("pay_success".equals(orderJson.getString("action"))) {

            String orderId = orderJson.getJSONObject("order").getString("order_id");
            String accountId = orderJson.getJSONObject("order").getString("account_id");

            DouyinOrderRequest douyinOrderRequest = new DouyinOrderRequest();
            douyinOrderRequest.setAccountId(accountId);
            douyinOrderRequest.setPageNum(1);
            douyinOrderRequest.setPageSize(1);
            douyinOrderRequest.setOrderId(orderId);
            ArrayList<AjaxResult> ajaxResults = douYinOrderService.createOrder(douyinOrderRequest);
            AjaxResult.success(ajaxResults);
            log.info("抖音端订单录入系统完毕，{}",ajaxResults);
            }

            return Math.toIntExact(slkjDouyinRecall.getId());
        } else if ("life_product_audit".equals(slkjDouyinRecall.getEvent())) {
//            商品审核结果同步

        }


        return Math.toIntExact(slkjDouyinRecall.getId());
    }

    /**
     * 修改抖音回调数据记录
     *
     * @param douyinProductAuditRequest 抖音回调数据记录
     * @return 结果
     */
    @Override
    public int updateSlkjDouyinRecall(DouyinProductAuditRequest douyinProductAuditRequest) {
        douyinProductAuditRequest.setUpdateTime(DateUtils.getNowDate());
        SlkjDouyinRecall slkjDouyinRecall = new SlkjDouyinRecall();
        BeanUtils.copyProperties(douyinProductAuditRequest, slkjDouyinRecall);
        slkjDouyinRecall.setContent(JSON.toJSONString(douyinProductAuditRequest.getContent()));
        return slkjDouyinRecallMapper.updateSlkjDouyinRecall(slkjDouyinRecall);
    }

    /**
     * 批量删除抖音回调数据记录
     *
     * @param ids 需要删除的抖音回调数据记录主键
     * @return 结果
     */
    @Override
    public int deleteSlkjDouyinRecallByIds(Long[] ids) {
        return slkjDouyinRecallMapper.deleteSlkjDouyinRecallByIds(ids);
    }

    /**
     * 删除抖音回调数据记录信息
     *
     * @param id 抖音回调数据记录主键
     * @return 结果
     */
    @Override
    public int deleteSlkjDouyinRecallById(Long id) {
        return slkjDouyinRecallMapper.deleteSlkjDouyinRecallById(id);
    }
}
